package cn.itsource.ymcc.producer;

import cn.itsource.ymcc.constant.CourseConstants;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CourseProducerImpl implements CourseProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    private boolean flag = false;


    @Override
    public void sendSMSMessage(List<Long> courseIds) {
        rocketMQTemplate.sendOneWay(CourseConstants.TOPIC_SMS_MESSAGE_NAME+":"+CourseConstants.TAGS_SMS_MESSAGE_NAME,
                MessageBuilder.withPayload(JSON.toJSONString(courseIds)).build());

        /*rocketMQTemplate.asyncSend(CourseConstants.TOPIC_SMS_MESSAGE_NAME+":"+CourseConstants.TAGS_SMS_MESSAGE_NAME,
                MessageBuilder.withPayload(JSON.toJSONString(courseIds)).build(), new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println(222);
                    }

                    @Override
                    public void onException(Throwable throwable) {

                    }
                });*/
    }

    @Override
    public void sendEmailMessage(List<Long> courseIds) {
        rocketMQTemplate.sendOneWay(CourseConstants.TOPIC_EMAIL_MESSAGE_NAME+":"+CourseConstants.TAGS_EMAIL_MESSAGE_NAME,
                MessageBuilder.withPayload(JSON.toJSONString(courseIds)).build());
        /*rocketMQTemplate.asyncSend(CourseConstants.TOPIC_EMAIL_MESSAGE_NAME + ":" + CourseConstants.TAGS_EMAIL_MESSAGE_NAME,
                MessageBuilder.withPayload(JSON.toJSONString(courseIds)).build(), new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println(111);
                    }

                    @Override
                    public void onException(Throwable throwable) {

                    }
                });*/
    }

    @Override
    public void sendSystemMessage(List<Long> courseIds) {
        rocketMQTemplate.sendOneWay(CourseConstants.TOPIC_SYSTEM_MESSAGE_NAME+":"+CourseConstants.TAGS_SYSTEM_MESSAGE_NAME,
                MessageBuilder.withPayload(JSON.toJSONString(courseIds)).build());

        /*rocketMQTemplate.asyncSend(CourseConstants.TOPIC_SYSTEM_MESSAGE_NAME+":"+CourseConstants.TAGS_SYSTEM_MESSAGE_NAME,
                MessageBuilder.withPayload(JSON.toJSONString(courseIds)).build(), new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println(333);
                    }

                    @Override
                    public void onException(Throwable throwable) {

                    }
                });*/
    }

    @Override
    public void excuteAll(List<Long> courseIds) {
        sendSMSMessage(courseIds);
        sendEmailMessage(courseIds);
        sendSystemMessage(courseIds);
    }


}